//
//  ViewController.m
//  CoreAnimation
//
//  Created by LG on 2018/5/17.
//  Copyright © 2018年 iqiyi. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *viewAnimate;

@property (nonatomic, assign) CGRect frameOriginal;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.frameOriginal = self.viewAnimate.frame;
    
    CALayer *layer = [[CALayer alloc]init];
    layer.frame = CGRectMake(10, 10, 40, 80);
    layer.backgroundColor = [[UIColor redColor]CGColor];
    [self.view.layer addSublayer:layer];
    
    CGPoint position = layer.position;
    CGPoint anchorPoint = layer.anchorPoint;
    // postion.x = frame.origin.x + anchorPoint.x * frame.width
    // postion.y = frame.origin.y + anchorPoint.y * frame.height
    NSLog(@"");
    
    layer.anchorPoint = CGPointMake(1., 1.);
    CGPoint positionAfter= layer.position;
    CGPoint anchorPointAfter = layer.anchorPoint;
    NSLog(@"");
    
    // frame 和 anchorPoint的关系
    /*
     anchorPoint : .5 , .5
     frame : 10 , 10 , 40 , 80
     position : 30 , 50
     
     anchorPoint : 1 , 1
     frame : -10 , -30 , 40 , 80
     position : 30 , 50
     
     */
}



- (IBAction)btnClickAnimate:(UIButton *)sender
{
    [self shakeAnimation];
    
//    CGRect rect = self.viewAnimate.frame;
//    self.viewAnimate.layer.anchorPoint = CGPointMake(1, 1.);
//    self.viewAnimate.frame = rect;
//    CGFloat width = CGRectGetWidth(rect);
//    CGFloat height = CGRectGetHeight(rect);
//
//    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
//    scaleAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0,0)];
//    scaleAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, width, height)];
//    scaleAnimation.duration = .4;
//    scaleAnimation.fillMode = kCAFillModeForwards;
//    scaleAnimation.removedOnCompletion = NO;
//
//    [self.viewAnimate.layer addAnimation:scaleAnimation forKey:@"ssss"];
}

- (IBAction)btnClickReset:(UIButton *)sender {
    self.viewAnimate.layer.anchorPoint = CGPointMake(.5, .5);
    self.viewAnimate.frame = self.frameOriginal;
}

- (IBAction)btnClick:(UIButton *)sender {
    NSLog(@"%s",__PRETTY_FUNCTION__);
}

#pragma mark --
#pragma mark -- UIView Animaion
- (void)viewTransitionAnimation
{
    static NSInteger i = 0;
    [UIView beginAnimations:@"view1" context:nil];
    [UIView setAnimationDuration:.35];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationWillStartSelector:@selector(animationWillStart)];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop)];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.viewAnimate cache:NO];
    i++;
    UIColor *color = i%2==0? [UIColor redColor]:[UIColor yellowColor];
    self.viewAnimate.backgroundColor = color;
    [UIView commitAnimations];
}

- (void)animationWillStart
{
    NSLog(@"%s",__PRETTY_FUNCTION__);
}

- (void)animationDidStop
{
     NSLog(@"%s",__PRETTY_FUNCTION__);
}

- (void)viewBlockAnimation
{
    CGRect bounds = self.view.bounds;
    static NSInteger i = 0;
    [UIView animateWithDuration:10 delay:.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        i ++;
        CGPoint point = i % 2 == 0? CGPointMake(CGRectGetMidX(bounds), 0):CGPointMake(CGRectGetMidX(bounds), CGRectGetMaxY(bounds));
        self.viewAnimate.center = point;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)shakeAnimation
{
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"transform.rotation";
//    anim.values = @[@(angle2Radio(-5)),@(angle2Radio(5)),@(angle2Radio(-5))];
    
    anim.repeatCount = MAXFLOAT;
    
    //自动反转
    //anim.autoreverses = YES;
    
    [self.viewAnimate.layer addAnimation:anim forKey:nil];
}

@end
